<template>
   <t-space direction="vertical">
	   <t-space><t-input v-model="article.title" placeholder="请输入姓名"></t-input>
	   <t-button @click="save">保存</t-button>
	   <!-- <t-button theme="default" @click="">新增</t-button> -->
	   </t-space>
	   <Tinymce ref="editor" v-model="article.html" @save="save" width="100%" />
   </t-space>
</template>
<script type="ts">
export default {
  name: 'archivesEdit',
};
</script>
<script setup>
import { ref ,computed,watch} from 'vue'
  import { useRouter,useRoute  } from 'vue-router';
import { MessagePlugin } from 'tdesign-vue-next';
import Tinymce from '../../components/editor/tinymce/index.vue';
import wordConvert from '../../components/wordConvert.vue';
import {saveArchive,deleteArchive,getArchive} from '@/api/archives'

const route=useRoute()
const postsId=ref('')
const content = ref('')
const editor=ref(null)
const article=ref({
	id:'',
	title:'',
	html:''
})
const uploadSuccessHandler=(ret)=>{
	console.log('uploadSuccessHandler',JSON.stringify(ret))
	content.value=ret
	editor.value.setContent(content.value);
  }
  
  const fetchData=()=>{
	  let id=route.params.id
	  console.log('route.params '+JSON.stringify(route.params))
	  console.log('route.params.id='+route.params.id)
	  getArchive(id)
	  .then(ret=>{
		  article.value=ret
	  })
  }
 const save=(s)=>{ 
    if (article.value.title == '') {
    	MessagePlugin.error('标题不能为空')
    	return
    }
	//console.log('content '+editor.value.getContent())
	article.value.html=editor.value.getContent()
	
    article.value.type='archive'
    saveArchive(article.value)
    .then(ret=>{
    	//console.log(JSON.stringify(ret))
      article.value.id=ret.id;
    	MessagePlugin.success('保存成功') 
       
    }).catch(err=>{
    	MessagePlugin.error(err.data)
    })
  }
const insert=()=>{
	article.value.id=''
	article.value.title=''
	article.value.html=''
	editor.value.setContent('')
}
watch(()=>route.params.id,
()=>{
	if(route.params.id!=article.value.id)
	{ 
		fetchData()
	}
})

</script>
 